﻿/*
    Parker for Kaixin001.com 

    Copyright (C) 2008, TurboZV, turbozv@gmail.com, http://www.turbozv.com/

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

using System;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace Helper
{
    public interface IRunner
    {
        /// <summary>
        /// 加载程序相关配置文件 *.ini
        /// </summary>
        /// <param name="filename"></param>
        void LoadConfig(string filename);

        /// <summary>
        /// 运行程序
        /// </summary>
        /// <param name="globalConfigData">全局配置(Config.txt)</param>
        /// <returns></returns>
        bool Run(GlobalConfigData globalConfigData);
    }

    public static class Runner
    {
        public static void Start(IRunner runer)
        {
            _runer = runer;

            try
            {
                LoadAndRun();

                while (_config.RepeatTime > 0)
                {
                    int restTime = _config.RepeatTime;
                    while (restTime > 0)
                    {
                        TraceLog.PrintLn("Wait {0} hours to restart...", restTime--);//modified by LanceZhang, change the mins to hours
                        Thread.Sleep(60 * 1000 * 60);
                    }

                    LoadAndRun();
                }
            }
            catch (Exception e)
            {
                TraceLog.Error(e.Message);
            }
        }

        private static void LoadAndRun()
        {
            ReadGlobalConfig();
            TraceLog.WriteToFile = _config.WriteToLogFile;

            string appName = Process.GetCurrentProcess().MainModule.FileName;
            string[] filelist = Directory.GetFiles(Path.GetDirectoryName(appName), "*.ini");//目录下所有的ini文件
            Array.Sort(filelist);
            foreach (string filename in filelist)
            {
                TraceLog.PrintLn("您已经配置了账号文件: " + filename);
            }
            foreach (string filename in filelist)
            {
                try
                {
                    TraceLog.PrintLn("读取账号文件: " + filename);
                    
                    _runer.LoadConfig(filename);

                    for (int i = 0; i < _config.TryTimes; i++)
                    {
                        if (_runer.Run(_config))
                        {
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    TraceLog.Error(e.Message);
                }
            }
        }

        private static void ReadGlobalConfig()
        {
            try
            {
                string filename = _appDirectory + "\\" + c_ConfigFile;

                if (!File.Exists(filename))
                {
                    TraceLog.PrintLn("缺少{0}文件，系统加载默认配置.", filename);
                    return;
                }

                _config = GlobalConfigHelper.ReadGlobalConfigData(filename);
            }
            catch (Exception e)
            {
                TraceLog.Error(e.Message);
            }
        }

        #region Member Fields
        private const string c_ConfigFile = "Config.txt";
        private static string _appDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
        private static GlobalConfigData _config = new GlobalConfigData();
        private static IRunner _runer;
        #endregion
    }
}
